package com.huatao.practice.server.service.impl;

import com.huatao.practice.api.enums.SubjectInfoTypeEnum;
import com.huatao.practice.api.vo.SpecialPracticeCategoryVo;
import com.huatao.practice.api.vo.SpecialPracticeLabelVo;
import com.huatao.practice.api.vo.SpecialPracticeVo;
import com.huatao.practice.server.entity.dto.CategoryDTO;
import com.huatao.practice.server.entity.po.CategroyPO;
import com.huatao.practice.server.entity.po.LabelCountPO;
import com.huatao.practice.server.entity.po.PrimaryCategoryPO;
import com.huatao.practice.server.entity.po.SubjectLabelPO;
import com.huatao.practice.server.mapper.SubjectCategoryDao;
import com.huatao.practice.server.mapper.SubjectLabelDao;
import com.huatao.practice.server.mapper.SubjectMappingDao;
import com.huatao.practice.server.service.PracticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

@Service
@Slf4j
public class PracticeServiceImpl implements PracticeService {

    @Resource
    private SubjectCategoryDao subjectCategoryDao;

    @Resource
    private SubjectMappingDao subjectMappingDao;

    @Resource
    private SubjectLabelDao subjectLabelDao;

    @Override
    public List<SpecialPracticeVo> getSpecialPracticeContent() {
        List<SpecialPracticeVo> specialPracticeVoList = new LinkedList<>();
        List<Integer> subjectTypeList = new LinkedList<>();
        subjectTypeList.add(SubjectInfoTypeEnum.RADIO.getCode());
        subjectTypeList.add(SubjectInfoTypeEnum.MULTIPLE.getCode());
        subjectTypeList.add(SubjectInfoTypeEnum.JUDGE.getCode());
        CategoryDTO categoryDTO = new CategoryDTO();
        categoryDTO.setSubjectTypeList(subjectTypeList);
        List<PrimaryCategoryPO> poList = subjectCategoryDao.getPrimaryCategory(categoryDTO);
        if (CollectionUtils.isEmpty(poList)) {
            return specialPracticeVoList;
        }
        poList.forEach(primaryCategoryPO -> {
            SpecialPracticeVo specialPracticeVo = new SpecialPracticeVo();
            specialPracticeVo.setPrimaryCategoryId(primaryCategoryPO.getId());
            CategroyPO categroyPO = subjectCategoryDao.selectById(primaryCategoryPO.getParentId());
            specialPracticeVo.setPrimaryCategoryName(categroyPO.getCategoryName());
            CategoryDTO categoryDTOTemp = new CategoryDTO();
            categoryDTOTemp.setCategoryType(2);
            categoryDTOTemp.setParentId(primaryCategoryPO.getId());
            List<CategroyPO> smallPoList = subjectCategoryDao.selectList(categoryDTOTemp);
            if (CollectionUtils.isEmpty(smallPoList)) {
                return;
            }
            List<SpecialPracticeCategoryVo> categoryVoList = new LinkedList<>();
            smallPoList.forEach(smallPo -> {
                List<SpecialPracticeLabelVo> labelVOList = getLabelVOList(smallPo.getId(), subjectTypeList);
                if (CollectionUtils.isEmpty(labelVOList)) {
                    return;
                }
                SpecialPracticeCategoryVo specialPracticeCategoryVo = new SpecialPracticeCategoryVo();
                specialPracticeCategoryVo.setCategoryId(smallPo.getId());
                specialPracticeCategoryVo.setCategoryName(smallPo.getCategoryName());
                List<SpecialPracticeLabelVo> labelList = new LinkedList<>();
                labelList.forEach(labelVO -> {
                    SpecialPracticeLabelVo specialPracticeLabelVo = new SpecialPracticeLabelVo();
                    specialPracticeLabelVo.setId(labelVO.getId());
                    specialPracticeLabelVo.setAssembleId(labelVO.getAssembleId());
                    specialPracticeLabelVo.setLabelName(labelVO.getLabelName());
                    labelList.add(specialPracticeLabelVo);
                });
                specialPracticeCategoryVo.setLabelList(labelList);
                categoryVoList.add(specialPracticeCategoryVo);
            });
            specialPracticeVo.setCategoryVoList(categoryVoList);
            specialPracticeVoList.add(specialPracticeVo);
        });
        return specialPracticeVoList;
    }

    private List<SpecialPracticeLabelVo> getLabelVOList(Long categoryId, List<Integer> subjectTypeList) {
        List<LabelCountPO> countPOList = subjectMappingDao.getLabelSubjectCount(categoryId, subjectTypeList);
        if (CollectionUtils.isEmpty(countPOList)) {
            return Collections.emptyList();
        }
        List<SpecialPracticeLabelVo> voList = new LinkedList<>();
        countPOList.forEach(countPo -> {
            SpecialPracticeLabelVo vo = new SpecialPracticeLabelVo();
            vo.setId(countPo.getLabelId());
            vo.setAssembleId(categoryId + "-" + countPo.getLabelId());
            SubjectLabelPO subjectLabelPO = subjectLabelDao.queryById(countPo.getLabelId());
            vo.setLabelName(subjectLabelPO.getLabelName());
            voList.add(vo);
        });
        return voList;
    }
}
